package com.winchain.chainbill.bill.biz.transfer.rx;

import com.shell.framework.command.ChainBase;
import com.shell.framework.command.CommandContext;
import com.shell.framework.rx.OnErrorNotImplementedSubscriber;
import com.winchain.chainbill.bill.base.po.CbOpt;
import com.winchain.chainbill.bill.base.po.CbRec;
import com.winchain.chainbill.bill.base.po.CbXfe;
import com.winchain.chainbill.bill.biz.Bill;
import com.winchain.chainbill.bill.biz.contract.cmd.ContractSponsorCmd;
import com.winchain.chainbill.bill.biz.send.cmd.BillAttCmd;
import com.winchain.chainbill.bill.biz.send.cmd.BillInvoiceCmd;
import com.winchain.chainbill.bill.biz.send.cmd.BillMoveCmd;
import com.winchain.chainbill.bill.biz.send.cmd.BillOptCmd;
import com.winchain.chainbill.bill.biz.transfer.cmd.BillDfrlCmd;
import com.winchain.chainbill.bill.biz.transfer.cmd.BillExtCmd;
import com.winchain.chainbill.bill.biz.transfer.cmd.BillXfeCmd;
import com.winchain.chainbill.bill.biz.transfer.vo.BillSplitEvent;
import com.winchain.chainbill.bill.biz.transfer.vo.BillXfe;
import java.math.BigDecimal;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class BillXfeChdSubscriber extends OnErrorNotImplementedSubscriber<BillSplitEvent<CbXfe>> {
	@Autowired
	private BillOptCmd<CbXfe> billOptCmd;
	@Autowired
	private BillExtCmd<CbXfe> billExtCmd;
	@Autowired
	private BillXfeCmd billXfeCmd;
	@Autowired
	private BillMoveCmd<CbXfe> billMoveCmd;
	@Autowired
	private BillDfrlCmd<CbXfe> billDeferralCmd;
	@Autowired
	private BillAttCmd<CbXfe> billAttCmd;
	@Autowired
	private ContractSponsorCmd<CbXfe> contractSponsorCmd;
	@Autowired
	private BillInvoiceCmd<CbXfe> billInvoiceCmd;

	public void onApplicationEvent(BillSplitEvent<CbXfe> t) {
		List<BillXfe> bills = (List<BillXfe>) t.getChildren();
		CbXfe cbXfe = new CbXfe();
		BigDecimal xfeAmt = new BigDecimal(0);
		for (BillXfe bill : bills) {
			CommandContext<Bill<CbXfe>> context = new CommandContext();
			context.setSource(bill);
			CbOpt opt = new CbOpt();
			opt.setCurState("S005");
			opt.setOptCode(null);
			opt.setRemark("��������");

			context.setValue("Operation", opt);
			context.setValue("Deferral", bill.getDfrl());
			ChainBase<Bill<CbXfe>> crb = new ChainBase();
			if (bill.getSource() != null) {
				opt.setLimDate(((CbXfe) bill.getSource()).getXfeDate());
				xfeAmt = xfeAmt.add(bill.getRec().getBillAmt());
				cbXfe = (CbXfe) bill.getSource();
				context.setValue("bizId", cbXfe.getXfeId());
				context.setValue("invoiceNo", cbXfe.getInvoiceNo());
				context.setValue("attInvoices", cbXfe.getAttInvoicesJson());
				context.setValue("invType", t.getValue("invType"));
				context.setValue("contractFileIds", cbXfe.getContractFileIds());
				crb.add(this.billAttCmd);
				crb.add(this.billInvoiceCmd);
				crb.add(this.contractSponsorCmd);
			}
			crb.add(this.billOptCmd);
			crb.add(this.billExtCmd);
			crb.add(this.billXfeCmd);
			crb.add(this.billMoveCmd);
			crb.add(this.billDeferralCmd);

			crb.execute(context);
		}
		t.setValue("xfeAmt", xfeAmt);
		t.setValue("cbXfe", cbXfe);
	}
}
